package com.example.nearby;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.os.Build;

public class ApplicationActivity extends ActionBarActivity {
	protected int page;
	protected int viewID;
	protected String currentPageName;
	protected List<Integer> pages;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		viewID = -1;
		currentPageName = "main";
		Button b = (Button)findViewById(R.id.imageView1);
		if(b != null)
			setButtonDestination(b, "home");
	}
	public void setPageName(String name) {
		currentPageName = name;
	}
	
	
	/**
	 * 
	 * @param b (Button) - The button to add the event to
	 * @param activityName - The name of the activity to be called
	 * Maps a button to a navigation activity. When the button is clicked the page will
	 * be routed to the new activity, calling doBeforeFilter before loading it.
	 */
	protected void setButtonDestination(Button b, String activityName) {
		Class activity = mapActivityToClass(activityName);
		if(activity != null)
			b.setOnClickListener(new ButtonListener(activity));
	}
	
	protected Class mapActivityToClass(String activityName) {
		Class activity = null;
		if(activityName.equals("login"))
			activity = com.example.nearby.LoginActivity.class;
		else if(activityName.equals("home"))
			activity = com.example.nearby.NearbyController.class;
		else if(activityName.equals("product"))
			activity = com.example.nearby.ProductActivity.class;
		else if(activityName.equals("storefront"))
			activity = com.example.nearby.StorefrontActivity.class;
		else if(activityName.equals("submit"))
			activity = com.example.nearby.SubmitActivity.class;
		else if(activityName.equals("guest"))
			activity = com.example.nearby.GuestActivity.class;
		else if(activityName.equals("submit"))
			activity = com.example.nearby.SubmitActivity.class;
		else if(activityName.equals("view"))
			activity = com.example.nearby.ViewStorefrontActivity.class;
		return activity;
	}
	
	protected void gotoActivity(String activityName) {
		Class activity = mapActivityToClass(activityName);
		if(activity != null) {
			Intent intent = new Intent(getApplicationContext(), activity); 
	    	startActivity(intent);
		}
	}
	
	public class ButtonListener implements View.OnClickListener
	{
		private Class target;
		public ButtonListener(Class c) {
			target = c;
		}
		public void onClick(View v) {
			if(doBeforeFilter((Button)v)) {
				showPage(target);
				System.out.println("testing nav login");
			}
		}
		private void showPage(Class c)
	    {
			System.out.println("Changing activity");
	    	Intent intent = new Intent(getApplicationContext(), c); 
	    	startActivity(intent);
	    }
	}
	
	public void setPagination() {
		System.out.println("Message");
		page = 1;
		int counter = 1;
		pages = new ArrayList<Integer>();
		while(true) {
			int id = getResId(currentPageName + "_page" + counter);
			System.out.println("Page found: " + id);
			if(id != 0) {
				pages.add(id);
				View v = findViewById(id);
				try {
					if(counter == 1) {
						v.setVisibility(View.VISIBLE);
					} else {
						v.setVisibility(View.GONE);
					}
				} catch(NullPointerException e) {
					System.out.println("V is null!");
				}
			} else {
				break;
			}
			counter++;
		}
		System.out.println("Message2");
		//ViewGroup view = (ViewGroup)findViewById(R.id.pages);
		//totalPages = view.getChildrenCount();
	}
	
	public void changePage(boolean forward) {
		if((forward && page < pages.size()) || (!forward && page > 1)) {
			View v = findViewById(pages.get(page-1));
			v.setVisibility(View.GONE);
			if(forward) {
				this.page++;
			} else {
				this.page--;
			}
			v = findViewById(pages.get(page-1));
			v.setVisibility(View.VISIBLE);
		}
		if(page == pages.size()) {
			int id = getResId(currentPageName + "_submit");
			Button v = (Button)findViewById(id);
			v.setText("Submit");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.nearby_controller, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public boolean doBeforeFilter(Button source) {
		return true;
	}
	
	public int getResId(String name) {
		System.out.println("Trying " + name);
		int id = getResources().getIdentifier(name, "id", getPackageName());
		System.out.println("Found id: " + id);
		return id;
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(
					R.layout.fragment_nearby_controller, container, false);
			return rootView;
		}
	}
	private List<String[]> dbGetAllStorefronts() {
		List<String[]> stores = new ArrayList<String[]>(); 
		String[][] all = new String[][]{
				{"0", "StoreE1"}, 
				{"1", "StoreA1"}, 
				{"2", "StoreA2"}, 
				{"3", "LocationB1"}, 
				{"4", "LocationB2"}, 
				{"5", "PlaceC1"}, 
				{"6", "PlaceC2"}, 
				{"7", "TestD1"}, 
				{"8", "TestD2"}, 
				{"9", "StoreE2"}
		};
		for(int i=0; i<all.length; i++) {
			stores.add(all[i]);
		}
		return stores;
	}
	
	private List<String[]> dbGetAllUsers() {
		List<String[]> users = new ArrayList<String[]>(); 
		String[][] all = new String[][]{
				{"0", "djd89", "password"}, 
				{"1", "user", "password"}, 
				{"2", "admin", "password"}
		};
		for(int i=0; i<all.length; i++) {
			users.add(all[i]);
		}
		return users;
	}
	
	public List<String[]> getStoresByName(String name) {
		List<String[]> allStores = dbGetAllStorefronts();
		List<String[]> results = new ArrayList<String[]>(); 
		for (int i=0; i<allStores.size(); i++) {
			String[] store = allStores.get(i);
			if(store[1].startsWith(name)) {
				results.add(store);
			}
		}
		return results;
	}
	
	public String[] getAllStorefrontNames() {
		List<String[]> allStores = dbGetAllStorefronts();
		String[] results = new String[allStores.size()];
		for (int i=0; i<allStores.size(); i++) {
			String[] store = allStores.get(i);
			results[i] = store[1];
		}
		return results;
	}
	
	public int getStoreIDFromName(String name) {
		name = name.toLowerCase();
		List<String[]> allStores = dbGetAllStorefronts();
		for (int i=0; i<allStores.size(); i++) {
			String[] store = allStores.get(i);
			if(name.equals(store[1].toLowerCase())) {
				return Integer.parseInt(store[0]);
			}
		}
		return -1;
	}
	
	public String getStoreNameFromID(int id) {
		List<String[]> allStores = dbGetAllStorefronts();
		for (int i=0; i<allStores.size(); i++) {
			String[] store = allStores.get(i);
			if(id == Integer.parseInt(store[0])) {
				return store[1];
			}
		}
		return "N/A";
	}
	
	public boolean validateUser(String username, String password) {
		return true;
	}

}
